package com.base.vistter.iframe.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.vistter.iframe.entity.SysArea;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.mapper.SysAreaMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
@Slf4j
public class SysAreaService extends BaseService<SysAreaMapper, SysArea> {

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean save(SysArea entity) throws IframeException {
        boolean result = super.save(entity);
        if (entity.getParentId() == null) {
            entity.setParentPath(String.valueOf(entity.getId()));
        } else {
            SysArea parent = super.getById(entity.getParentId());
            entity.setParentPath(parent.getParentPath() + PARENT_PATH_DIVIDE + entity.getId());
        }
        super.updateById(entity);
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateById(SysArea entity) throws IframeException {
        SysArea persistence = super.getById(entity.getId());
        if(entity.getParentId() == null){
            entity.setParentPath(String.valueOf(entity.getId()));
            if (!Objects.equals(persistence.getParentId(), entity.getParentId())) {
                this.updateChildParentPath(persistence.getParentPath(), entity);
            }
        }else{
            SysArea parent = super.getById(entity.getParentId());
            entity.setParentPath(parent.getParentPath() + PARENT_PATH_DIVIDE + entity.getId());
            if (!Objects.equals(persistence.getParentId(), entity.getParentId())) {
                this.updateChildParentPath(persistence.getParentPath(), entity);
            }
        }
        return super.updateById(entity);
    }
    private void updateChildParentPath(String parentPath, SysArea sysArea) throws IframeException {
        this.baseMapper.updateParentPath(parentPath, sysArea.getParentPath());
    }

    @Transactional(readOnly = true)
    public List<SysArea> city() throws IframeException{
        LambdaQueryWrapper<SysArea> lambdaQueryWrapper = new LambdaQueryWrapper<SysArea>();
        lambdaQueryWrapper.likeLeft(SysArea::getCode , "00000000");
        return super.selectList(lambdaQueryWrapper);
    }

    @Transactional(readOnly = true)
    public List<SysArea> text(String code) throws IframeException{
        String[] ids = code.split(",");
        List<SysArea> list = new ArrayList<SysArea>();
        for(int i = 0 ; i < ids.length ; i++){
            SysArea sysArea = super.getById(Integer.parseInt(ids[i]));
            list.add(sysArea);
        }
        return list;
    }
    @Transactional(readOnly = true)
    public SysArea getSysAreaByCode(String code) throws IframeException{
        LambdaQueryWrapper<SysArea> lambdaQueryWrapper = new LambdaQueryWrapper<SysArea>();
        lambdaQueryWrapper.eq(SysArea::getCode , code);
        return super.selectOne(lambdaQueryWrapper);
    }
}
